package org.linphone.yinpin;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ViewFlipper;

public class ViewFlipperWrapper extends ViewFlipper implements OnGestureListener{
	
	private GestureDetector mGestureDetector;  

	public ViewFlipperWrapper(Context context, AttributeSet attrs) {
		super(context, attrs);
		mGestureDetector = new GestureDetector(this); 
	}
	
	@Override
	public void showNext() {
		super.showNext();
		if(mOnNextShowListener != null){
			mOnNextShowListener.onNext();
		}
	}
	
	@Override
	public void showPrevious() {
		super.showPrevious();
		if(mOnNextShowListener != null){
			mOnNextShowListener.onPre();
		}
	}
	
//	@Override
//	public boolean dispatchTouchEvent(MotionEvent ev) {
//		return super.dispatchTouchEvent(ev);
//	}
	
//	@Override
//	public boolean onInterceptTouchEvent(MotionEvent ev) {
//		if(ev.getAction() == MotionEvent.)
//		return super.onInterceptTouchEvent(ev);
//	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}
	
	private OnNextShowListener mOnNextShowListener;
	
	public void setOnNextShowListener(OnNextShowListener onNextShowListener){
		this.mOnNextShowListener = onNextShowListener;
	}
	
	public static interface OnNextShowListener{
		public void onNext();
		public void onPre();
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(e1.getX() > e2.getX()) {//向左滑动  
            showNext();    
       }else if(e1.getX() < e2.getX()) {//向右滑动  
           showPrevious();     
       }else {     
           return false;     
       }     
       return true;  
	}

}
